﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RuleCode.Table;
using System.IO;

namespace map
{
    public partial class designer : System.Web.UI.Page
    {
        protected string SystemDefaultShopBanner = string.Empty ;
        public static string DefaultImgPath = "/img/orange_banner.png";

        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof (SwfUploadFile ));
            SystemDefaultShopBanner = ResolveUrl("~"+DefaultImgPath );
            if (!IsPostBack)
            {
                dealrequest();
                InitLoginInfo();
                InitShopInfo();
                //ClearFiles();
            }
        }

        #region 登录信息相关
        /// <summary>
        /// 自动登录
        /// </summary>
        private void dealrequest()
        {
            cbuser.dealcookie();
            if (AliApi.appUserId == "")
            {
                Response.Redirect(ResolveUrl("~/member/login.aspx") + "?redirecturl=" + this.Request.Url);
            }
        }

        /// <summary>
        /// 初始化 登录信息
        /// </summary>
        protected void InitLoginInfo()
        {
            if (this.Request.Url.AbsolutePath != ResolveUrl("~/member/login.aspx")
                    && this.Request.Url.AbsolutePath != ResolveUrl("~/member/regist1.aspx"))
            {
                if (AliApi.appUserId == "")
                {
                    HyperLink_login.Text = "登录";
                    HyperLink_login.NavigateUrl = ResolveUrl("~/member/login.aspx") + "?redirecturl=" + this.Request.Url;

                    HyperLink_logout.NavigateUrl = ResolveUrl("~/member/regist1.aspx") + "?redirecturl=" + this.Request.Url;
                    HyperLink_logout.Text = "注册";
                }
                else
                {
                    HyperLink_login.Text = AliApi.appNickName;
                    HyperLink_login.NavigateUrl = "#";
                    HyperLink_logout.NavigateUrl = ResolveUrl("~/member/loginout.aspx") + "?redirecturl=" + this.Request.Url;
                    HyperLink_logout.Text = "注销";
                }
            }
            else
            {
                HyperLink_login.Text = "登录";
                HyperLink_login.NavigateUrl = ResolveUrl("~/member/login.aspx");

                HyperLink_logout.NavigateUrl = ResolveUrl("~/member/regist1.aspx");
                HyperLink_logout.Text = "注册";
            }
        }
        #endregion

        #region shop信息
        protected void InitShopInfo()
        {
            cbshop shop = cbshop.Find("cbuserid='"+AliApi.appUserId +"'");
            if (shop.Count > 0)
            {
                if (shop.cbshopbannerpic.Value.Trim() == string.Empty)
                {
                    CurrentBannerImg.Style["display"] = "none";
                    NoBannerSpan.Style["display"] = "block";
                }   
                else
                {
                    CurrentBannerImg.Style["display"] = "block";
                    NoBannerSpan.Style["display"] = "none";
                    CurrentBannerImg.Src = shop.cbshopbannerpic.Value;
                }
            }
        }
        #endregion

        #region 文件相关          
        public void ClearFiles()
        {
            Session.Clear();
        }
        #endregion

    }
}


public class SwfUploadFile
{
    public static string UploadFilePath = "~/swf/upload/";

    #region 文件相关
    [AjaxPro.AjaxMethod]
    public string SaveFiles(string id)
    {
        cbshop shop = cbshop.Find("cbuserid='"+AliApi.appUserId +"'");
        if (shop.Count < 0)
            return string.Empty ;

        //使用默认图片
        if (id == string.Empty)
        {
            shop.cbshopbannerpic.Value = map.designer .DefaultImgPath ;
            shop.cbitembannerpic.Value = map.designer.DefaultImgPath;
            shop.Modify();
            return map.designer.DefaultImgPath;
        }

        //使用上传图片
        if (HttpContext.Current. Session["file_info"] != null)
        {
            List<Thumbnail> thumbnails = HttpContext.Current.Session["file_info"] as List<Thumbnail>;

            string UploadPath = HttpContext.Current.Server.MapPath(UploadFilePath);

            foreach (Thumbnail img in thumbnails)
            {
                if (img.ID == id)
                {
                    try
                    {
                        shop.cbshopbannerpic.Value = "/swf/upload/" + img.ID + "." + img.FileType;
                        shop.cbitembannerpic.Value = "/swf/upload/" + img.ID + "." + img.FileType;
                        shop.Modify();

                        return "/swf/upload/" + img.ID + "."+img.FileType ;
                    }
                    catch (Exception e)
                    {
                    }
                }
            }

            ClearFiles();
        }
        return string.Empty;
    }

    [AjaxPro.AjaxMethod]
    public void ClearFiles()
    {
        HttpContext.Current.Session.Remove("file_info");
    }
    #endregion

    
}